<template>
	<view class="nas-login">
		<view class="nas-login-img"><image src="/static/nas-static/nas-login-icon.svg" mode=""></image></view>
		<view class="nas-login-title"><image src="/static/nas-static/nas-title.svg" mode=""></image></view>
		<view class="nas-login-password">
			<nas-input
				ref="input1"
				type="password"
				:verification="['isNull', 'isznumBigSmallZifu']"
				:verificationTip="['密码不能为空', '密码不规范，请重新输入']"
				placeholder="请输入密码"
				maBtm="10"
			></nas-input>
		</view>
		<view class="nas-login-password">
			<nas-input ref="input2" type="password" :verification="['isNull']" :verificationTip="['密码不能为空']" placeholder="请确认密码" maBtm="10"></nas-input>
		</view>
		<view class="nas-login-rule">密码必须为8-20位数字、大小写字母和字符的组合</view>
		<button class="nas-login-button" @click="onerification">进入云NAS</button>
	</view>
</template>

<script>
import request from '../utils/request.js';
import nasAes from '../utils/nasAes.js';
import nasInput from '../components/nas-input/nas-input';
export default {
	data() {
		return {};
	},
	components: {
		nasInput
	},
	methods: {
		async onerification() {
			if (this.$refs.input1.getValue()) {
				if (this.$refs.input2.getValue()) {
					if (this.$refs.input1.getValue() !== this.$refs.input2.getValue()) {
						uni.showToast({
							icon: 'none',
							title: '两次密码不一致'
						});
					} else {
						// 获取加密需要的key
						const keyParams = {
							method: 'POST',
							url: '/nas/session/key',
							data: {
								mobile: uni.getStorageSync('nasPhone')
							}
						};
						const keyRes = await request(keyParams);
						const key = keyRes.data;
						// 注册
						const loginToken =
							uni.getStorageSync('token') ||
							'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJwaG9uZU51bWJlciI6IjEzMjYyNzQwNjk0IiwidXNlcklkIjo1ODgyNCwiaWF0IjoxNjU2MzE5OTIyMTY2LCJsb2dpblNvdXJjZSI6Il9hcHBfbG9naW5fc291cmNlXyJ9.rk03fvRA3D2rw07trUg0Xaz-Y7Y_x0wJcHq68yYNFgg';
						const registerParams = {
							method: 'POST',
							url: '/naslocal/user/appregister',
							data: {
								mobile: uni.getStorageSync('nasPhone'),
								password: nasAes.setAES(this.$refs.input1.getValue(), key),
								loginToken: nasAes.setAES(loginToken, key)
							}
						};
						console.log(registerParams, 'registerParams');
						const resRegister = await request(registerParams);
						console.log(resRegister, 'resRegister');
						if (!resRegister) return;
						// 获取token进行跳转
						const tokenParams = {
							method: 'POST',
							url: '/naslocal/sdk/apptoken',
							data: {
								mobile: uni.getStorageSync('nasPhone'),
								loginToken: nasAes.setAES(loginToken, key)
							}
						};
						console.log(tokenParams, 'tokenParams');
						const resToken = await request(tokenParams);
						if (resToken.data.access_token) {
							uni.setStorageSync('nasToken', resToken.data.access_token);
							uni.navigateTo({
								url: '/pages/nas/nas-home/nas-home'
							});
						} else {
							uni.showToast({
								icon: 'error',
								title: '获取token失败'
							});
						}
						// uni.navigateTo({
						// 	url: `/pages/nas/nas-home/nas-home`
						// });
					}
				}
			}
		}
	}
};
</script>

<style lang="scss" scoped>
.nas-login {
	position: absolute;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	padding-top: 108rpx;
	background: #fff;
	overflow: hidden;
	.nas-login-img {
		text-align: center;
		image {
			width: 176rpx;
			height: 108rpx;
		}
	}
	.nas-login-title {
		margin-top: 44rpx;
		text-align: center;
		image {
			width: 296rpx;
			height: 48rpx;
		}
	}
	.nas-login-password {
		margin: 44rpx 95rpx;
	}
	.nas-login-button {
		margin: 120rpx 95rpx 0 95rpx;
		height: 80rpx;
		background: #3069dc;
		border-radius: 40rpx;
		color: #fff;
		font-family: HarmonyOS_Sans_SC;
		font-size: 32rpx;
		color: #ffffff;
		text-align: center;
		font-weight: 400;
	}
	.nas-login-rule {
		text-align: center;
		margin: 21rpx 134rpx;
		font-family: HarmonyOS_Sans_SC;
		font-size: 24rpx;
		color: #808080;
		text-align: justify;
		font-weight: 400;
	}
}
</style>
